package com.example.eos.tool;

import com.example.eos.bean.User;
import com.example.eos.mapper.UserMapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 认证授权
 */
public class AuthRealm extends AuthorizingRealm {

    @Autowired
    private UserMapper userMapper;

    //认证.登录
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken utoken=(UsernamePasswordToken) token;//获取用户输入的token
        String phone = utoken.getUsername();
        User user = userMapper.findByPhone(phone);
        return new SimpleAuthenticationInfo(user, user.getPassword(),this.getClass().getName());//放入shiro.调用CredentialsMatcher检验密码
    }

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal){
//        User user=(User) principal.fromRealm(this.getClass().getName()).iterator().next();//获取session中的用户
        String per = "权限";
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        info.addStringPermission(per);//将权限放入shiro中.
        return info;
    }

}
